//
//  StatusCellFrame.swift
//  微博项目
//
//  Created by admin on 16/3/10.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit
let statusCellMargin:CGFloat = 10
let statusCellHeaderNameFontSize:CGFloat = 12
let statusCellHeaderDateFontSize:CGFloat = 10
let statusCellHeaderSourceFontSize:CGFloat = 9
let statusCellBodyTextFontSize:CGFloat = 13
let ScreenW = UIScreen.mainScreen().bounds.size.width
let ScreenH = UIScreen.mainScreen().bounds.size.height
let PhotoWH:CGFloat = 70
class StatusCellFrame: NSObject {
    var status:Status
    var cellH:CGFloat?
    /**
     呢称部分
    */
    var headerImageFrame:CGRect?
    var headerNameFrame:CGRect?
    var headerVipFrame:CGRect?
    var headerDateFrame:CGRect?
    var headerSourceFrame:CGRect?
    var headerFrame:CGRect?
    /**
     内容部分
     */
    var bodyFrame:CGRect?
    var bodyTextFrame:CGRect?
    var bodyImagesFrame:[CGRect]?
    var bodyImageViewFrame:CGRect?
    var reTweetedFrame:CGRect?
    var reTweetedNameFrame:CGRect?
    var reTweetedTextFrame:CGRect?
    
    private init(statusPrivate:Status) {
        self.status = statusPrivate
    }
    convenience init(status:Status) {
        self.init(statusPrivate:status)
        self.setFrame()
    }
    func setFrame()
    {
        self.setHeaderFrame()
        self.setBodyFrame()
        self.setToolFrame()
    }
    /**
     呢称部分计算
     */
    func setHeaderFrame()
    {
        // 头像
        let headerImageW:CGFloat = 35
        let headerImageH:CGFloat = 35
        let headerImageX = statusCellMargin
        let headerImagey = statusCellMargin
        self.headerImageFrame = CGRectMake(headerImageX, headerImagey, headerImageW, headerImageH)
        // 名字
        let headerNameSize = self.status.user.name.sizeWithAttributes([NSFontAttributeName : UIFont.systemFontOfSize(statusCellHeaderNameFontSize)])
        let headerNameX = CGRectGetMaxX((self.headerImageFrame)!) + statusCellMargin
        let headerNamey = headerImagey
        self.headerNameFrame = CGRectMake(headerNameX, headerNamey, headerNameSize.width, headerNameSize.height)
        // 会员
        let headerVipW:CGFloat = 14
        let headerVipH:CGFloat = 14
        let headerVipX = CGRectGetMaxX((self.headerNameFrame)!) + statusCellMargin
        let headerVipy = headerImagey
        self.headerVipFrame = CGRectMake(headerVipX, headerVipy, headerVipW, headerVipH)
        
        // 来源
        let headerSourceSize = self.status.source.sizeWithAttributes([NSFontAttributeName :
            UIFont.systemFontOfSize(statusCellHeaderSourceFontSize)])
        self.headerSourceFrame = CGRectMake(0, 0, headerSourceSize.width, headerSourceSize.height)
        // 总大小
        let headerW:CGFloat = UIScreen.mainScreen().bounds.size.width
        let headerH:CGFloat = CGRectGetMaxY(self.headerImageFrame!) + statusCellMargin
        let headerX:CGFloat = 0
        let headery:CGFloat = 30
        self.headerFrame = CGRectMake(headerX, headery, headerW, headerH)
    }
    func setBodyFrame()
    {
        // 内容
        let bodyTextSize = (self.status.text as NSString).boundingRectWithSize(CGSizeMake(ScreenW - 2 * statusCellMargin, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName :
            UIFont.systemFontOfSize(statusCellBodyTextFontSize)], context: nil).size
        let bodyTextX = statusCellMargin
        let bodyTexty = statusCellMargin
        self.bodyTextFrame = CGRectMake(bodyTextX, bodyTexty, bodyTextSize.width, bodyTextSize.height)
        // 总大小
        let bodyW:CGFloat = ScreenW
        let bodyH:CGFloat = CGRectGetMaxY(self.bodyTextFrame!) + statusCellMargin
        let bodyX:CGFloat = (self.headerFrame?.origin.x)!
        let bodyY:CGFloat = CGRectGetMaxY(self.headerFrame!)
        self.bodyFrame = CGRectMake(bodyX, bodyY, bodyW, bodyH)
        if self.status.retweeted_status != nil
        {
            self.setReTweetedStatus()
        }
        else// 没有转发的微博的话，可能会有配图
        {
            if self.status.pic_urls.count > 0
            {
                self.setImagesFrame(self.status.pic_urls.count,height: CGRectGetMaxY(self.bodyTextFrame!))
                self.bodyFrame = CGRectMake(self.bodyFrame!.origin.x, self.bodyFrame!.origin.y, (self.bodyFrame?.size.width)!,( self.bodyFrame?.size.height)! + self.bodyImageViewFrame!.size.height)
                
            }
        }
    }
    /**
     计算图片的frame
     */
    func setImagesFrame(imageCount:Int,height:CGFloat)
    {
        var colum = 0
        if imageCount <= 4
        {
            colum = 2
        }
        else
        {
            colum = 3
        }
        var temp:[CGRect] = []
        for var i = 0;i < imageCount;i++
        {
            let tempA = CGFloat(i % colum)
            let tempB = CGFloat(i / colum)
            let tempRect = CGRectMake((tempA) * statusCellMargin + (tempA * PhotoWH), (tempB + 1) * statusCellMargin + (tempB * PhotoWH), PhotoWH, PhotoWH)
            temp.append(tempRect)
        }
        self.bodyImagesFrame = temp
        self.bodyImageViewFrame = CGRectMake(statusCellMargin, height, CGFloat(colum) * PhotoWH + (CGFloat(colum) + 1) * statusCellMargin, CGRectGetMaxY((self.bodyImagesFrame?.last)!) + statusCellMargin)
    }
    /**
     计算转发微博的frame
    */
    func setReTweetedStatus()
    {
        self.reTweetedNameFrame = CGRect.zero
        if self.status.retweeted_status.user?.retweetedUserName != nil
        {
            // 名字
            let reTweetedNameSize = self.status.retweeted_status.user?.retweetedUserName.sizeWithAttributes([NSFontAttributeName : UIFont.systemFontOfSize(statusCellHeaderNameFontSize)])
            let reTweetedNameX:CGFloat = 0
            let reTweetedNameY = statusCellMargin
            self.reTweetedNameFrame = CGRectMake(reTweetedNameX, reTweetedNameY, (reTweetedNameSize?.width)!, (reTweetedNameSize?.height)!)
        }
        // 内容
        let reTweetedTextSize = (self.status.retweeted_status.text as NSString).boundingRectWithSize(CGSizeMake(ScreenW - 2 * statusCellMargin, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName :
            UIFont.systemFontOfSize(statusCellBodyTextFontSize)], context: nil).size
        let reTweetedTextX = self.reTweetedNameFrame?.origin.x
        let reTweetedTextY = CGRectGetMaxY(self.reTweetedNameFrame!) + statusCellMargin
        self.reTweetedTextFrame = CGRectMake(reTweetedTextX!, reTweetedTextY, reTweetedTextSize.width, reTweetedTextSize.height)
        // 总大小
        let bodyW:CGFloat = ScreenW - 2 * statusCellMargin
        let bodyH:CGFloat = CGRectGetMaxY(self.reTweetedTextFrame!) + statusCellMargin
        let bodyX:CGFloat = (self.headerImageFrame?.origin.x)!
        let bodyY:CGFloat = CGRectGetMaxY(self.bodyTextFrame!)
        self.reTweetedFrame = CGRectMake(bodyX, bodyY, bodyW, bodyH)
        //
        if self.status.retweeted_status.pic_urls.count > 0
        {
            self.setImagesFrame(self.status.retweeted_status.pic_urls.count,height: CGRectGetMaxY(self.reTweetedTextFrame!))
            self.reTweetedFrame = CGRectMake(self.reTweetedFrame!.origin.x, self.reTweetedFrame!.origin.y, (self.reTweetedFrame?.size.width)!,( self.reTweetedFrame?.size.height)! + self.bodyImageViewFrame!.size.height)
        }
        self.bodyFrame = CGRectMake(self.bodyFrame!.origin.x, self.bodyFrame!.origin.y, (self.bodyFrame?.size.width)!, (self.bodyFrame?.size.height)! + self.reTweetedFrame!.size.height)
    }
    /**
     工具栏部分
     */
    var toolFrame:CGRect?
    var toolRetweetFrame:CGRect?
    var toolCommentFrame:CGRect?
    var toolUnlikeFrame:CGRect?
    var toolLineA:CGRect?
    var toolLineB:CGRect?
    func setToolFrame()
    {
        let buttonW = ScreenW / 3
        let buttonH:CGFloat = 35
        self.toolRetweetFrame = CGRectMake(0, 0, buttonW, buttonH)
        self.toolCommentFrame = CGRectMake(CGRectGetMaxX(self.toolRetweetFrame!), 0, buttonW, buttonH)
        self.toolLineA = CGRectMake(self.toolCommentFrame!.origin.x, 0, 2, buttonH)
        self.toolUnlikeFrame = CGRectMake(CGRectGetMaxX(self.toolCommentFrame!), 0, buttonW, buttonH)
        self.toolLineB = CGRectMake(self.toolUnlikeFrame!.origin.x, 0, 2, buttonH)
        self.toolFrame = CGRectMake(0, CGRectGetMaxY(self.bodyFrame!), ScreenW, buttonH)
        self.cellH = CGRectGetMaxY(self.toolFrame!)
    }
}
